package guidePage;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.xuegengwang.xuegengwang.MainActivity;
import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.SPUtils;
import utils.VariousUtils;

/**
 * Created by admin on 2017/2/15.
 * 第一开机引导页
 */
@ContentView(R.layout.guide_page)
public class GuidePage extends BaseActivity {
    @ViewInject(R.id.guide_page_viewPager)
    private ViewPager viewPager;

//    private Button btn_enter;//进入主页的按钮

    private View view1, view2, view3, view4;
    @ViewInject(R.id.guide_page4_doc1)
    private ImageView ima1;
    @ViewInject(R.id.guide_page4_doc2)
    private ImageView ima2;
    @ViewInject(R.id.guide_page4_doc3)
    private ImageView ima3;
    //    @ViewInject(R.id.guide_page4_doc4)
//    private ImageView ima4;
    private List<View> viewList = new ArrayList<>();//view数组
    private LayoutInflater layoutInflater;
    private List<ImageView> docView = new ArrayList<>();//点点数组


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            initViewPager();
        }

        private void initViewPager() {
            layoutInflater = getLayoutInflater();
            view1 = layoutInflater.inflate(R.layout.guide_page1, null);
            view2 = layoutInflater.inflate(R.layout.guide_page2, null);
            view3 = layoutInflater.inflate(R.layout.guide_page3, null);
//        view4 = layoutInflater.inflate(R.layout.guide_page4,null);
            viewList.add(view1);//装要切换的view
            viewList.add(view2);
            viewList.add(view3);
//        viewList.add(view4);
            docView.add(ima1);//装点点
            docView.add(ima2);
            docView.add(ima3);
//        docView.add(ima4);
            viewPager.setAdapter(new PagerAdapter() {
                @Override
                public int getCount() {
                    return viewList.size();
                }

                @Override
                public boolean isViewFromObject(View view, Object object) {
                    return view == object;
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                    container.removeView(viewList.get(position));
                }

                @Override
                public Object instantiateItem(ViewGroup container, int position) {
                    container.addView(viewList.get(position));
                    return viewList.get(position);
                }
            });

            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    changeDoc(position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

            view3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                SharedPreferences sharedPreferences = SPUtils.getSharedPreferences();
//                if (!TextUtils.isEmpty(sharedPreferences.getString(Constant.USER_ID, ""))) {
                    //如果已经登录
                    Intent i = new Intent(GuidePage.this, MainActivity.class);
                    startActivity(i);
                    GuidePage.this.finish();
//                } else {
//                    //未登录
//                    Intent i = new Intent(GuidePage.this, LoginPage.class);
//                    i.putExtra("isGuidePage", true);//告诉登录页，是启动页叫他的
//                    startActivity(i);
//                    GuidePage.this.finish();
//                }
                }
            });
        }

        private void changeDoc(int position) {
            for (int i = 0; i < docView.size(); i++) {
                if (i == position) {
                    docView.get(i).setImageResource(R.mipmap.main_doc);
                } else {
                    docView.get(i).setImageResource(R.mipmap.main_doc1);
                }
            }
        }


//
//    //获取手机信息
//    private String getVersion() {
//        //获取应用版本号
//        try {
//            PackageManager manager = this.getPackageManager();
//            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
//            String version = info.versionName;
//            return version;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "没有版本号";
//        }
//    }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            viewList = null;
            layoutInflater = null;
            docView = null;
        }
    }

